/***********************************************
 * Title:       UserService.java
 * Description: UserService.java
 * Author:      NiDeveloper
 * Create Date: 2010-6-1
 * CopyRight:   CopyRight(c)@2009
 * Company:     TJUSCS
 * Version:     1.0
 ***********************************************
 */
package com.younker.waf.core.user;

import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.infohazard.maverick.flow.ControllerContext;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.younker.waf.core.sec.RuntimeUser;
import com.younker.waf.core.sec.SecurityConfig;
import com.younker.waf.db.IBatisConfig;

/**
 * @author NiDeveloper
 *
 */
public class UserService
{
        
        private SqlMapClient sqlMap = IBatisConfig.getSqlMapInstance();
        
        private static User NULL_USER;
        private static Logger log = Logger.getLogger(UserService.class);
        
        private UserService()
        {
                NULL_USER = new User();
                NULL_USER.setId((long)0);               
                
        }
        private static UserService _instance;
        static 
        {
                _instance = new UserService();
        }
        
        public static UserService getInstance()
        {
                return _instance;
        }
        
        public User getCurrentUser( ControllerContext ctx )
        {
                RuntimeUser runtimeUser = (RuntimeUser) ctx.getRequest().getSession().getAttribute(SecurityConfig.USER);
                long userId = runtimeUser.getId();
                log.debug("userId=" + userId);
                if( userId == 0)
                {
                        return NULL_USER;
                }
                User user;
                try
                {
                        user = (User) sqlMap.queryForObject("selectUserById", userId);
                } catch (SQLException e)
                {
                        log.error("select user by id error", e);
                        return NULL_USER;
                }
                return user;
        }
        
}
